package com.xyz.tasks.bf;

import com.free.common.vo.ResultModel;
import com.free.exception.SystemException;
import com.free.utils.DateUtils;
import com.xyz.domain.ext.EnumPeriodStatus;
import com.xyz.domain.table.SLottery;
import com.xyz.domain.table.SLotteryPeriod;
import com.xyz.service.LotteryInfoService;
import com.xyz.service.LotteryPeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Component
public class LotteryGenBf {

    @Autowired
    LotteryInfoService lotteryInfoService;

    @Autowired
    LotteryPeriodService lotteryPeriodService;

    private static final String DT_YMD_HMS = "%s %s:00";


    public ResultModel genLotteries(){
        Date nextDate = DateUtils.nextDate(new Date(),1);
        List<SLottery> lotteryInfos = lotteryInfoService.getAllLotteryInfos();
        lotteryInfos.forEach(x->{
            if(x.getType() == 1){
                try {
                    genLottery(x, nextDate);
                } catch (SystemException e) {
                    e.printStackTrace();
                }
            }
        });
        return ResultModel.success();
    }

    public SLottery lotteryInfo(String lotteryId){
        return lotteryInfoService.getLotteryInfo(lotteryId);
    }

    @Transactional
    public ResultModel genLottery(SLottery lotteryInfo, Date current) throws SystemException {

        String yyyyMMddStr = DateUtils.convert2String(current, DateUtils.FormatEnum.yyyy_MM_dd);
        String dayStartTime = String.format(DT_YMD_HMS, yyyyMMddStr,lotteryInfo.getDayStartTime());
        String dayEndTime = String.format(DT_YMD_HMS, yyyyMMddStr, lotteryInfo.getDayEndTime());
        Date dayStartInfo = DateUtils.convert2Date(dayStartTime, DateUtils.FormatEnum.yyyy_MM_dd_HH_mm_ss);
        Date dayEndInfo = DateUtils.convert2Date(dayEndTime, DateUtils.FormatEnum.yyyy_MM_dd_HH_mm_ss);
        SLotteryPeriod period = new SLotteryPeriod();
        period.setLotteryId(lotteryInfo.getId());
        int currentPeriod = lotteryPeriodService.getLotteryMaxPeriod(period);
        while(dayStartInfo.compareTo(dayEndInfo) <= 0){
            currentPeriod = currentPeriod + 1;
            SLotteryPeriod genRes = genPeriod(lotteryInfo,dayStartInfo,currentPeriod,lotteryInfo.getStepSecs(),lotteryInfo.getStepMins());
            dayStartInfo = genRes.getEndTime();
        }
        return ResultModel.success();
    }

    private SLotteryPeriod genPeriod(SLottery lottery, Date periodStartTime, int periodNum,int seconds, int minutes){
        Date current = new Date();
        SLotteryPeriod period = new SLotteryPeriod();
        period.setLotteryId(lottery.getId());
        period.setLotteryCode(lottery.getCode());
        period.setCreateTime(current);
        period.setCreator(1);
        period.setStatus(EnumPeriodStatus.UNSTART.getKey());
        period.setPeriod(periodNum);
        period.setStartTime(periodStartTime);
        period.setEndTime(nextDateInfo(periodStartTime, seconds, minutes));
        period = lotteryPeriodService.addPeriodInfo(period);
        return period;
    }

    public static Date nextDateInfo(Date currentInfo, int seconds, int minutes){
        if(seconds != 0){
            currentInfo = nextSecond(currentInfo, seconds);
        }
        if(minutes != 0){
            currentInfo = DateUtils.nextMinute(currentInfo, minutes);
        }
        return currentInfo;
    }
    public static Date nextSecond(Date currentDate, int seconds) {
        if (null == currentDate) {
            return null;
        } else {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(currentDate);
            calendar.add(Calendar.SECOND, seconds);
            return calendar.getTime();
        }
    }



}
